﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using SPVisualDev.VSAddin.Dialogs;

namespace SPVisualDev.VSAddin
{
    public partial class SettingsPageWSP : UserControl, IDTToolsOptionsPage
    {
        internal const string C_PARAM_WSP_BUILDER_PATH = "WSPBuilderPath";

        private bool m_info_msg_shown;
        private DTE m_dte;

        public SettingsPageWSP()
        {
            InitializeComponent();

            this.txtWSPBuilderPath.GotFocus += new EventHandler(txtWSPBuilderPath_GotFocus);
        }

        void txtWSPBuilderPath_GotFocus(object sender, EventArgs e)
        {
            CheckWSPToolsExist();
        }

        private void CheckWSPToolsExist()
        {
            if (m_info_msg_shown)
                return;

            string wsp_builder_path = Common.GetUserRegistryParam(C_PARAM_WSP_BUILDER_PATH, false);
            if (string.IsNullOrEmpty(wsp_builder_path))
            {
                if (!string.IsNullOrEmpty(Common.GetInstallationKey(C_PARAM_WSP_BUILDER_PATH, false)))
                    wsp_builder_path = Common.GetInstallationKey(C_PARAM_WSP_BUILDER_PATH, false);
            }

            if (string.IsNullOrEmpty(wsp_builder_path))
            {
                if (string.IsNullOrEmpty(Common.GetWSPToolsPath()))
                {
                    MessageBox.Show("You do not have to configure this section since you already have WSPBuilderExtensions installed.", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    m_info_msg_shown = true;
                }
            }
        }

        #region IDTToolsOptionsPage Members

        public void GetProperties(ref object PropertiesObject)
        {
        }

        public void OnAfterCreated(DTE DTEObject)
        {
            try
            {
                m_dte = DTEObject;                

                string wsp_builder_path = Common.GetUserRegistryParam(C_PARAM_WSP_BUILDER_PATH, false);
                if (string.IsNullOrEmpty(wsp_builder_path))
                {
                    if (!string.IsNullOrEmpty(Common.GetInstallationKey(C_PARAM_WSP_BUILDER_PATH, false)))
                        wsp_builder_path = Common.GetInstallationKey(C_PARAM_WSP_BUILDER_PATH, false);
                }


                if (!string.IsNullOrEmpty(wsp_builder_path))
                    if (!wsp_builder_path.ToLower().EndsWith("wspbuilder.exe"))
                        wsp_builder_path += wsp_builder_path.EndsWith("\\") ? "WSPBuilder.exe" : "\\WSPBuilder.exe";


                txtWSPBuilderPath.Text = wsp_builder_path;

            }
            catch (Exception ex)
            {
              DialogError.ShowError(ex);
            }
        }

        public void OnCancel()
        {
            throw new NotImplementedException();
        }

        public void OnHelp()
        {
            throw new NotImplementedException();
        }

        public void OnOK()
        {
            try
            {
                Common.SetUserRegistryParam(C_PARAM_WSP_BUILDER_PATH, txtWSPBuilderPath.Text);
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex, "Couldn't set WSP builder path in current user registry.", false);
            }

        }

        #endregion

        private void btnWSPBuilderPath_Click(object sender, EventArgs e)
        {
            try
            {
                CheckWSPToolsExist();

                DialogResult res = openFileDialog.ShowDialog(this);

                if (res == DialogResult.OK)
                {
                    txtWSPBuilderPath.Text = openFileDialog.FileName;
                }
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }


        }
    }
}
